﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using RhotCMS.Models;
using Utils;

namespace RhotCMS
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            var initialRootController = CMSSettingsRepository.GetSingleAppSetting("InitialRootController");

            #region Administrarion

            routes.MapRoute(
              name: "Administration",
              url: "Administration/{action}",
              defaults: new { controller = "Administration", action = "Index" }
          );

            routes.MapRoute(
              name: "Administration/Store/Action",
              url: "Administration/Store/{action}/{id}",
              defaults: new { controller = "Administration", action = "Index", id = UrlParameter.Optional }
          );

            routes.MapRoute(
           name: "Media/FileUpload",
           url: "Media/FileUpload",
           defaults: new { controller = "Media", action = "FileUpload", id = UrlParameter.Optional }
       );

            routes.MapRoute(
          name: "Media/CropImage",
          url: "Media/CropImage",
          defaults: new { controller = "Media", action = "CropImage", id = UrlParameter.Optional }
      );

            routes.MapRoute(
         name: "Media/ResizeImage",
         url: "Media/ResizeImage",
         defaults: new { controller = "Media", action = "ResizeImage", id = UrlParameter.Optional }
     );

            #endregion
            //Pages
            if (Convert.ToBoolean(CMSSettingsRepository.GetSingleAppSetting("IsMultiLanguage")))
            {

                //                routes.MapRoute(
                //name: "Pages",
                //url: "{controller}/{action}",
                //defaults: new { controller = "Pages", action = "Index" }
                //);


                routes.MapRoute(
              name: "Account",
              url: "{lang}/Account/{action}",
              defaults: new { lang = UrlParameter.Optional, controller = "Account", action = UrlParameter.Optional }
          );

                routes.MapRoute(
              name: "Store",
              url: "{lang}/Store",
              defaults: new { lang = UrlParameter.Optional, controller = "Store", action = "Index" }
          );

                routes.MapRoute(
              name: "Store/Category",
              url: "{lang}/Store/Category/{id}",
              defaults: new { lang = UrlParameter.Optional, controller = "Store", action = "Category", id = UrlParameter.Optional }
          );

                routes.MapRoute(
   name: "Store/GetCart",
   url: "{lang}/Store/GetCart/{id}",
   defaults: new { lang = UrlParameter.Optional, controller = "Store", action = "GetCart", id = UrlParameter.Optional }
);
                routes.MapRoute(
             name: "Store/SetCart",
             url: "{lang}/Store/SetCart/{id}",
             defaults: new { lang = UrlParameter.Optional, controller = "Store", action = "SetCart", id = UrlParameter.Optional }
         );
                routes.MapRoute(
             name: "Store/AddToCart",
             url: "{lang}/Store/AddToCart/{id}",
             defaults: new { lang = UrlParameter.Optional, controller = "Store", action = "AddToCart", id = UrlParameter.Optional }
         );

                routes.MapRoute(
              name: "Root",
              url: "",
              defaults: new { controller = initialRootController, action = "Index" }
          );
                routes.MapRoute(
              name: "Root/lang",
              url: "{lang}",
              defaults: new { lang = UrlParameter.Optional, controller = initialRootController, action = "Index" }
          );
                routes.MapRoute(
              name: "Root/Pages",
              url: "{lang}/{name}",
              defaults: new { lang = UrlParameter.Optional, controller = "Pages", action = "Index" }
          );
                routes.MapRoute(
              name: "Group/Pages",
              url: "{lang}/{group}/{name}/{id}",
              defaults: new { lang = UrlParameter.Optional, controller = "Pages", action = "Index", id = UrlParameter.Optional }
          );
                routes.MapRoute(
                name: "Default",
                url: "{lang}/{controller}/{action}",
                defaults: new { lang = UrlParameter.Optional, controller = "Home", action = "Index" });
            }
            else
            {
                routes.MapRoute(
              name: "Account",
              url: "Account/{action}",
              defaults: new { controller = "Account", action = UrlParameter.Optional }
          );

                routes.MapRoute(
              name: "Store",
              url: "Store",
              defaults: new { controller = "Store", action = "Index" }
          );

                routes.MapRoute(
             name: "Store/Category",
             url: "Store/Category/{id}",
             defaults: new { controller = "Store", action = "Category", id = UrlParameter.Optional }
         );
                routes.MapRoute(
             name: "Store/GetCart",
             url: "Store/GetCart/{id}",
             defaults: new { controller = "Store", action = "GetCart", id = UrlParameter.Optional }
         );
                routes.MapRoute(
             name: "Store/SetCart",
             url: "Store/SetCart/{id}",
             defaults: new { controller = "Store", action = "SetCart", id = UrlParameter.Optional }
         );
                routes.MapRoute(
             name: "Store/AddToCart",
             url: "Store/AddToCart/{id}",
             defaults: new { controller = "Store", action = "AddToCart", id = UrlParameter.Optional }
         );

                routes.MapRoute(
              name: "Root",
              url: "",
              defaults: new { controller = initialRootController, action = "Index", group = "root", name = "index" }
          );
                routes.MapRoute(
               name: "Root/Pages",
               url: "{name}",
               defaults: new { controller = "Pages", action = "Index", group = "root", name = UrlParameter.Optional }
           );

                routes.MapRoute(
               name: "Group/Pages",
               url: "{group}/{name}",
               defaults: new { controller = "Pages", action = "Index", group = UrlParameter.Optional, name = UrlParameter.Optional }
           );

                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );

            }
        }
    }
}